package com.xqls.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.IntakeRealdataBean;
import com.xqls.biz.AdministrationBiz;
import com.xqls.biz.IntakeRealdataBiz;
import com.xqls.dal.model.IntakeRealdata;
import com.xqls.service.IntakeRealdataService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class IntakeRealdataBizImpl implements IntakeRealdataBiz {
	private static Logger logger = Logger.getLogger(IntakeRealdataBiz.class);
	@Autowired
	private IntakeRealdataService realdataService;
	@Autowired
	private AdministrationBiz administrationBiz;
	@Autowired
	private RedisCache<String, Object> redisCache;
	@Override
	public CommonResult saveIntakeRealdataList(List<IntakeRealdataBean> intakeRealdataBeans) {
		CommonResult commonResult = new CommonResult();
		boolean success = false;
		try {
			for (IntakeRealdataBean intakeRealdataBean : intakeRealdataBeans) {
				IntakeRealdataBean realdataBean = realdataService.queryByRealdataNo(intakeRealdataBean.getRealdataNo());
				if (realdataBean == null) {
					success = realdataService.insertIntakeRealdata(intakeRealdataBean);
				} else {
					success = realdataService.updateIntakeRealdata(intakeRealdataBean);
				}
			}
		} catch (Exception e) {
			logger.error("取水口实时数据批量保存异常", e);
		}
		commonResult.setIsSuccess(success);
		return commonResult;
	}

	@Override
	public CommonResult queryIntakeRealdata(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult commonResult = new CommonResult();
		try {
			List<String> columns_name = new ArrayList<String>();
			List<Object> columns = new ArrayList<Object>();
			List<String> displayfiled = administrationBiz.conversion("intake_realdata");
			columns.add("viewId");
			for (String display : displayfiled) {
				if(!display.equals("realdata_no")){
					columns.add(display);
				}
			}

			List<String> fieldalias = administrationBiz.fieldalias("intake_realdata");
			columns_name.add("序号");
			for (String fieldalia : fieldalias) {
				if(!fieldalia.equals("编号")){
					columns_name.add(fieldalia);
				}

			}

			
//			commonPage.setPageData(realdataService.selectByMap(commonPage.pageToMap()));
			List<Map<String, Object>> datas = realdataService.selectByMap(commonPage.pageToMap());
			int curr=commonPage.getCurr();
			for (int i = 0;i<datas.size();i++){
					datas.get(i).put("viewId", i + 1+((curr-1)*15));
			}
			commonPage.setPageData(datas);
			Map<String, Object> showMap = new HashMap<String, Object>();
			showMap.put("realdataList", commonPage.getPageData());
			showMap.put("userPerms", redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, SecurityUtils.getSubject().getPrincipal().toString())));

			showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
					columns.toArray(), null, "取水口实时数据"));
			commonPage.setHtml(VelocityUtil.getHtmlByMap("Realdata/realdataListData.vm", showMap,request,response,request.getSession().getServletContext()));
			commonResult.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("取水口实时数据查询异常", e);
		}
		return commonResult;
	}
	
	public List<Map<String, Object>> queryAll(){
		return realdataService.queryAll();
	}
	
	public IntakeRealdata selectByDeviceNo(String deviceNo){
		return realdataService.selectByDeviceNo(deviceNo);
	}

}
